package com.example.demo.contrller;

import com.example.demo.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/api/thymeleaf")
public class ThymeleafContrller {

    @Autowired
    private TestService service;

    // 访问 http://localhost:8080/api/demo/ 时渲染 index.html 模板
    @GetMapping("/")
    public String index(Model model) {
        // 向模板传递数据
        model.addAttribute("title", "Thymeleaf 集成示例");
        model.addAttribute("message", "这是从控制器传递的数据");
        return "index"; // 对应 templates/index.html
    }

    // 访问 http://localhost:8080/api/demo/2 时渲染 index.html 模板
    @GetMapping("/2")
    public String index() {
        return "forward:/index.html";
    }
}
